<template>
  <el-card>
    <div>
      <h3>应用上线</h3>
    </div>
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="git address">
        <el-input v-model="form.gitAddress"></el-input>
      </el-form-item>
      <el-form-item label="Applications:">
        <el-checkbox-group v-model="form.apps">
          <el-checkbox
            :label="item"
            v-for="(item, index) in form.apps"
            :key="index"
            >{{ item }}</el-checkbox
          >
        </el-checkbox-group>
      </el-form-item>
    </el-form>

    <el-steps :active="active" align-center>
      <el-step title="Git Clone" description="clone repository"></el-step>
      <el-step title="Docker Build" description="build images"></el-step>
      <el-step title="Helm Install" description="helm install apps"></el-step>
    </el-steps>

    <el-button style="margin-top: 12px" @click="next">{{ action }}</el-button>
  </el-card>
</template>
<script>
import { gitCloneRepo } from "@/network/git.js";
import { dockerBuildImages } from "@/network/images.js";
import {helmInstallApps} from "@/network/helm.js";
export default {
  name: "Apps",
  data() {
    return {
      active: 0,
      action: "Clone Repository",
      form: {
        gitAddress: "",
        apps: [],
      },
    };
  },
  methods: {
    next() {
      if (this.active == 0) {
        // clone
        gitCloneRepo({
          url: this.form.gitAddress,
        }).then((res) => {
          console.log(res);
          if (res.code == 200) {
            this.action = "Build Images";
            this.active++;
            this.form.apps = res.apps
          }
        });
      } else if (this.active == 1) {
        dockerBuildImages({
          apps: this.form.apps,
        }).then((res) => {
          if (res.code == 200) {
            this.action = "Helm Install Apps";
            this.active++;
          }
        });
      } else if (this.active == 2) {
        helmInstallApps({
          apps: this.form.apps,
        }).then((res) => {
          if (res.code == 200) {
            this.action = "Done";
            this.active++;
          }
        });
      } else {
        this.active = 0;
      }
    },
  },
  // },
};
</script>

<style>
.card-header {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 22px;
}

.text {
  font-size: 22px;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  width: 100%;
}
</style>